import {defineStore} from 'pinia';
import {queryRolePowerApi} from '@/api/login';
import pageCompoents from '@/router/pageCompoents';
import usePermission from '@/store/usePermission';

interface State {
    isCollapse: boolean;
    routeList: any;
    menuId?: any;
}

const fliterRouteList = (list: any[]): any[] => {
    const routesList: any[] = [];
    list.forEach((item) => {
        const {name, path, meta, children, component} = item;
        const route: any = {
            name,
            path,
            redirect: children.length > 0 ? `/${name}/${children[0].name}` : undefined,
            component: pageCompoents[component],
            meta,
        };
        if (name === 'bill_list' && meta.btnList.some((item: any) => item.id === 35)) {
            const detais = {
                name: 'bill_list_details',
                path: 'bill_list_details',
                component: () => import('@/views/AccountManage/BillList/BillListDetails/index.vue'),
                meta: {
                    sidebar: false,
                    title: 'Call车列表详情',
                    icon: 'list',
                    menuId: meta.menuId,
                },
            };
            routesList.push(detais);
        }
        if (children && children.length > 0) {
            route.children = fliterRouteList(children);
        }
        routesList.push(route);
    });
    return routesList;
};

// 过滤侧边栏菜单
function fliterMenuList(menuList: Array<any>): Array<any> {
    const menuArr: Array<any> = [];
    menuList.forEach((item) => {
        const {name, meta, children} = item;
        if (meta.sidebar) {
            const route: any = {name: meta.title, icon: meta.icon, path: name};
            if (children && children.length > 0) {
                route.children = fliterMenuList(children);
            }
            menuArr.push(route);
        }
    });
    return menuArr;
}

const useGlobalData = defineStore('globalData', {
    // id: 'globalData',
    state: (): State => {
        return {
            isCollapse: false,
            routeList: null,
            menuId: undefined,
        };
    },
    actions: {
        setIsCollapse() {
            this.isCollapse = !this.isCollapse;
        },
        setRouteList(routeList: State['routeList']) {
            this.routeList = routeList;
        },
        setMenuId(menuId: State['menuId']) {
            this.menuId = menuId;
        },
        // 获取侧边栏
        getMenuList() {
            return new Promise(async (resolve, reject) => {
                try {
                    const permission = usePermission();
                    const userRole = await queryRolePowerApi();
                    const list: any[] = userRole.data as any[];
                    const routes = fliterRouteList(list);
                    const menuList = fliterMenuList(routes);
                    permission.setMenuList(menuList);
                    const route = {
                        path: '/',
                        redirect: routes[0].path,
                    };
                    const nofound = [
                        {
                            path: '/404',
                            name: 'not-found',
                            component: () => import('@/views/NoFound/index.vue'),
                            meta: {
                                sidebar: false,
                            },
                        },
                        {
                            path: '/:pathMatch(.*)*',
                            redirect: '/404',
                        },
                    ];
                    const routeList: any = [route, ...routes, ...nofound];
                    this.setRouteList(routeList);
                    resolve(routeList);
                } catch (error) {
                    console.error(error);
                    reject();
                }
            });
        },
    },
});

export default useGlobalData;
